package _022_1_29

func checkIsEqual(strB byte, patternB byte) bool {
	if strB == patternB {
		return true
	} else if patternB == '?' {
		return true
	}

	return false
}

func checkIs(str string, pattern string, strId int, patternId int, isFind *bool) {

	if str == "" && len(pattern) == 1 && pattern[patternId] == '*' {
		*isFind = true
		return
	}
	if patternId == len(pattern)-1 && pattern[patternId] == '*' && strId == len(str) {
		*isFind = true
		return
	}

	if strId == len(str) && patternId == len(pattern) {
		*isFind = true
	}

	if patternId >= len(pattern) || strId >= len(str) {
		return
	}

	for strId < len(str) && patternId < len(pattern) {

		if checkIsEqual(str[strId], pattern[patternId]) {
			strId++
			patternId++
		} else if pattern[patternId] == '*' {
			if patternId == len(pattern)-1 {
				*isFind = true
				return
			}

			for strId < len(str) {
				if checkIsEqual(str[strId], pattern[patternId+1]) {
					checkIs(str, pattern, strId+1, patternId+2, isFind)
				}
				strId++
			}

		} else {
			return
		}

	}

	if strId == len(str) && patternId == len(pattern) {
		*isFind = true
	}

	if strId == len(str) && patternId == (len(pattern)-1) && pattern[patternId] == '*' {
		*isFind = true
	}

}

func IsMatch(s string, p string) bool {
	strB := ""
	if p != "" {
		for i := 0; i < len(p)-1; i++ {
			if p[i] == '*' && p[i+1] == '*' {
				continue
			} else {
				strB += string(p[i])
			}
		}

		if len(strB) == 0 || strB[len(strB)-1] != '*' {
			strB += string(p[len(p)-1])
		} else if p[len(p)-1] != '*' {
			strB += string(p[len(p)-1])
		}
	}

	isFind := false
	checkIs(s, strB, 0, 0, &isFind)
	return isFind
}
